#!/bin/bash

if [[ $(id -u) -ne 0 ]]; then
  echo "This script must be executed as root or using sudo."
  exit 99
fi

ARCH=$(dpkg --print-architecture)
dist="$(lsb_release --codename --short)"
SDIST=${dist,,}

if [[ "${SDIST}" = "bookworm" ]];then
  OMV_CODENAME=sandworm
elif [[ "${SDIST}" = "bullseye" ]];then
  OMV_CODENAME=shaitan
else
    echo "Unsupported architecture :: ${ARCH}"
    exit 5
fi


echo -e "nameserver 114.114.114.114\nnameserver 8.8.8.8" > /etc/resolv.conf

cat <<EOF > /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ ${SDIST} main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ ${SDIST}-updates main contrib non-free non-free-firmware
EOF

cat <<EOF > /etc/apt/sources.list.d/openmediavault-os-security.list
deb [signed-by=/usr/share/keyrings/debian-archive-${SDIST}-security-automatic.gpg] https://mirrors.tuna.tsinghua.edu.cn/debian-security ${SDIST}-security main contrib non-free non-free-firmware
deb-src [signed-by=/usr/share/keyrings/debian-archive-${SDIST}-security-automatic.gpg] https://mirrors.tuna.tsinghua.edu.cn/debian-security ${SDIST}-security main contrib non-free non-free-firmware
EOF

cat <<EOF > /etc/apt/sources.list.d/openmediavault-kernel-backports.list
deb [signed-by=/usr/share/keyrings/debian-archive-${SDIST}-automatic.gpg] https://mirrors.tuna.tsinghua.edu.cn/debian ${SDIST}-backports main contrib non-free non-free-firmware
EOF

apt-get update 
apt-get dist-upgrade -y

# add openmediavault-archive-keyring

apt-get install --yes gnupg sudo vim net-tools efibootmgr
wget -O "/etc/apt/trusted.gpg.d/openmediavault-archive-keyring.asc" https://packages.openmediavault.org/public/archive.key
apt-key add "/etc/apt/trusted.gpg.d/openmediavault-archive-keyring.asc"

install -m 0755 -d /etc/apt/keyrings
[ -f /etc/apt/keyrings/docker.gpg ]&& \rm -f /etc/apt/keyrings/docker.gpg

docker_key_url="https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg"
docker_key="/usr/share/keyrings/docker.gpg"
docker_list="/etc/apt/sources.list.d/omvdocker.list"

cat <<EOF > /etc/apt/sources.list.d/omvdocker.list
deb [signed-by=/usr/share/keyrings/docker.gpg arch=${ARCH} ] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian ${SDIST} stable
EOF
wget --quiet --output-document=- "${docker_key_url}" | gpg --dearmor > "${docker_key}"
chmod 644 ${docker_list} ${docker_key}
cp -rf ${docker_key}  /etc/apt/keyrings/docker.gpg


[ -f /etc/apt/keyrings/omvextras.gpg ]&& \rm -f /etc/apt/keyrings/omvextras.gpg
key_url="https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers/omvextras2026.asc"
key="/usr/share/keyrings/omvextras.gpg"
list="/etc/apt/sources.list.d/omvextras.list"

cat <<EOF > /etc/apt/sources.list.d/omvextras.list
deb [signed-by=/usr/share/keyrings/omvextras.gpg arch=${ARCH} ] https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers ${OMV_CODENAME} main
EOF
wget --quiet --output-document=- "${key_url}" | gpg --dearmor > "${key}"
chmod 644 ${list} ${key} 
cp -rf ${key} /etc/apt/keyrings/omvextras.gpg

cat <<EOF > /etc/apt/sources.list.d/openmediavault.list
deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/public ${OMV_CODENAME} main
deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/packages ${OMV_CODENAME} main
# deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://downloads.sourceforge.net/project/openmediavault/packages/ ${OMV_CODENAME} main
EOF

echo "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";" > /etc/apt/apt.conf.d/no-"${SDIST}"-firmware.conf

# install omv 7 
export LANG=C.UTF-8
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none

apt-get update
apt-get --yes --auto-remove --show-upgraded \
    --allow-downgrades --allow-change-held-packages \
    --no-install-recommends \
    --option Dpkg::Options::="--force-confdef" \
    --option DPkg::Options::="--force-confold" \
    install openmediavault-keyring openmediavault

apt clean all && apt update 
apt  -y  upgrade

source  /etc/profile

omv-confdbadm populate

echo -e "\n\n Openmediavault 7 installation has been completed"
echo -e " Accessing web at http://{Your_IP_Address} \n"

[ -f ./install ]&& bash ./install
